package tat.domain.impl;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import tat.domain.AllUseCases;
import tat.domain.TatActor;
import tat.domain.TatUseCase;
import tat.exception.ObjectAlreadyExistsException;

public class AllUseCasesImpl implements AllUseCases {

	private Map<String,TatUseCase> map = new HashMap<String, TatUseCase>();

    @Override
    public TatUseCase find(final String ucName) {
        return map.get(ucName);
    }

    @Override
    public String useCasesAsText() {
    	String text = "";
		Iterator<TatUseCase> useCases = map.values().iterator();
		while(useCases.hasNext()){
			text = text + useCases.next().toString();
		}
		return text;
    }

    @Override
    public String useCasesAsXML() {
    	String text = "";
		Iterator<TatUseCase> useCases = map.values().iterator();
		while(useCases.hasNext()){
			text = text + useCases.next().toXML();
		}
		return text;
    }

	@Override
	public void create(String name) throws ObjectAlreadyExistsException {
		if (map.containsKey(name)) {
			throw new ObjectAlreadyExistsException();
		}
		map.put(name, new TatUseCase(name));
	}

	@Override
	public void remove(String name) {
		map.remove(name);
	}

	@Override
	public TatUseCase[] conferUseCases() {
		return map.values().toArray(new TatUseCase[map.size()]);
	}

	@Override
	public void remapUseCase(String oldUCName, TatUseCase useCase) {
		map.remove(oldUCName);
		map.put(useCase.name(), useCase);
	}

}
